'use strict';
const { ORG_MAP, INDUSTRY_MAP, map2List, RECEIVER_LIST } = require('../dict');
const Controller = require('egg').Controller;

/**
 * @Controller 媒体模块
 */
class MediaController extends Controller {
  /**
   * @Summary 获取媒体机构列表
   * @Description 获取媒体机构列表
   * @Router get /media/org-list
   * @Response 200 orgListRes
   * */
  getOrgList() {
    const list = map2List(ORG_MAP, 'orgId', 'orgName');
    this.ctx.body = {
      code: 20000,
      data: list,
    };
  }

  /**
   * @Summary 获取媒体行业列表
   * @Description 获取媒体行业列表
   * @Router get /media/industry-list
   * @Response 200 industryListRes
   * */
  getIndustryList() {
    const list = map2List(INDUSTRY_MAP);
    this.ctx.body = {
      code: 20000,
      data: list,
    };
  }

  /**
   * @Summary 获取渠道列表
   * @Description 获取渠道列表
   * @Router get /media/receiver-list
   * @Response 200 receiverListRes
   * */
  getReceiverlList() {
    this.ctx.body = {
      code: 20000,
      data: RECEIVER_LIST,
    };
  }

  /**
   * @Summary 获取媒体列表
   * @Description 获取媒体列表
   * @Router get /media/list
   * @Request query string mediaName 名称
   * @Request query string mediaState 状态:NORMAL-正常,BANNED-停用
   * @Request query string orgId 所属机构
   * @Request query number pageNum 页码
   * @Request query number pageSize 每页条数
   * @Response 200 mediaListRes
   * */
  async getMediaList() {
    const { mediaName, mediaState, orgId, pageNum, pageSize } = this.ctx.request.query;
    const { rows: list, count } = await this.service.media.getList({
      mediaName, mediaState, orgId, pageNum, pageSize,
    });
    this.ctx.body = {
      code: 20000,
      count,
      list,
    };
  }

  /**
   * @Summary 媒体详情
   * @Description 媒体详情
   * @Router get /media/detail
   * @Request query number *mediaId 媒体id
   * @Response 200 mediaDetailRes
   * */
  async getDetail() {
    const { mediaId } = this.ctx.request.query;
    const media = await this.service.media.getDetail(mediaId);
    this.ctx.body = {
      code: 20000,
      data: media,
    };
  }

  /**
   * @Summary 新建媒体
   * @Description 新建媒体
   * @Router post /media/create
   * @Request body createMediaParams
   * */
  async create() {
    const mediaId = Date.now();
    const user = await this.service.media.create({
      ...this.ctx.request.body,
      mediaId,
      // mediaState: 'BANNED', // 默认停用
    });
    this.ctx.body = {
      code: 20000,
      data: user,
    };
  }

  /**
   * @Summary 更新媒体
   * @Description 更新媒体
   * @Router post /media/update
   * @Request body updateMediaParams
   * */
  async update() {
    const result = await this.service.media.update({
      ...this.ctx.request.body,
    });

    this.ctx.body = {
      code: result ? 20000 : -999,
      message: result ? '更新成功' : '参数不正确',
    };
  }

  /**
   * @Summary 添加关联渠道
   * @Description 添加关联渠道
   * @Router post /media/receiver-add
   * @Request body addReceiverParams
   * */
  async addReceiver() {
    const result = await this.service.media.addReceiver({
      ...this.ctx.request.body,
    });

    this.ctx.body = {
      code: result ? 20000 : -999,
      message: result ? '添加成功' : '参数不正确',
    };
  }

  /**
   * @Summary 更新关联渠道
   * @Description 更新关联渠道
   * @Router post /media/receiver-update
   * @Request body updateReceiverParams
   * */
  async updateReceiver() {
    const result = await this.service.media.updateReceiver({
      ...this.ctx.request.body,
    });

    this.ctx.body = {
      code: result ? 20000 : -999,
      message: result ? '更新成功' : '参数不正确',
    };
  }
}

module.exports = MediaController;
